package ikar.view.components;

import ikar.nrd.model.FillingRef;
import ikar.nrd.model.XsltNodeType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import org.apache.wicket.event.IEvent;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.string.Strings;

import com.inmethod.grid.IGridColumn;
import com.inmethod.grid.SizeUnit;
import com.inmethod.grid.column.CheckBoxColumn;
import com.inmethod.grid.column.editable.CheckBoxPropertyColumn;
import com.inmethod.grid.column.editable.DropDownChoiceColumn;
import com.inmethod.grid.column.editable.EditableCellPanel;
import com.inmethod.grid.column.editable.EditablePropertyColumn;
import com.inmethod.grid.column.editable.EditablePropertyTreeColumn;
import com.inmethod.grid.treegrid.TreeGrid;

/**
 * Page with {@link TreeGrid} that allows editing selected items.
 * 
 * @author Matej Knopp
 */
public class EditableTreeGridPanel extends Panel
{

	private static final long serialVersionUID = 1L;
	private Label selectionLabel;
	Boolean isChecket = false;
	private static String choiseStyle;
	private static int i1 = 0;
	/**
	 * Constructor.
	 */
	@SuppressWarnings({ "serial", "unchecked", "rawtypes" })
	public EditableTreeGridPanel(String id, DefaultTreeModel model)
	{
		
		super(id);
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		add(feedback);

		Form<?> form = new Form<Void>("form");
		add(form);
		List<IGridColumn<DefaultTreeModel, DefaultMutableTreeNode>> columns = new ArrayList<IGridColumn<DefaultTreeModel, DefaultMutableTreeNode>>();

		columns.add(new CheckBoxColumn<DefaultTreeModel, DefaultMutableTreeNode>("checkBox"));
		CheckBoxPropertyColumn chc = new CheckBoxPropertyColumn(Model.of("StyleRef"), "userObject.isStyleRefNeed") {


			@Override
			public boolean cellClicked(IModel rowModel) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) rowModel.getObject();
				return ((FillingRef) node.getUserObject()).isStyleRefNeed();
			}

		};
		chc.setMaxSize(35);

		columns.add(chc);
		// columns.add(new SubmitCancelColumn<DefaultTreeModel, DefaultMutableTreeNode>("edit", Model.of("Edit")));
		columns.add(new EditablePropertyTreeColumn<DefaultTreeModel, DefaultMutableTreeNode, String>(Model.of("Xpath"), "userObject.xpath") {
			@Override
			public String getCellCssClass(IModel rowModel, int rowNum) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) rowModel.getObject();
				String styleClass = ((FillingRef) node.getUserObject()).getXsdStyleClass();
				return Strings.isEmpty(styleClass) ? super.getCellCssClass(rowModel, rowNum) : styleClass;
			}

		});
		columns.add(new EditablePropertyColumn<DefaultTreeModel, DefaultMutableTreeNode, String>(Model.of("Plate Code"), "userObject.plateCode"));
		columns.add(new EditablePropertyColumn<DefaultTreeModel, DefaultMutableTreeNode, String>(Model.of("Parent Plate Code"), "userObject.parentPlateCode"));
		columns.add(new EditablePropertyColumn<DefaultTreeModel, DefaultMutableTreeNode, String>(Model.of("Name "), "userObject.name"));
		columns.add(new EditablePropertyColumn<DefaultTreeModel, DefaultMutableTreeNode, String>(Model.of("Fields Group Code   "), "userObject.fieldsGroup") {
		});
		columns.add(new DropDownChoiceColumn(Model.of("Style Field type"), "userObject.fieldType", Arrays.asList("B", "D", "S", "E", "F", "L", "N", "U")) {

			@Override
			protected EditableCellPanel newCellPanel(String componentId, IModel rowModel, IModel cellModel) {
				return super.newCellPanel(componentId, rowModel, Model.of("S"));
			}

		});
		columns.add(new EditablePropertyColumn<DefaultTreeModel, DefaultMutableTreeNode, String>(Model.of("Level"), "userObject.level"));
		columns.add(new EditablePropertyColumn<DefaultTreeModel, DefaultMutableTreeNode, String>(Model.of("Sublevel"), "userObject.subLevel"));
		columns.add(new DropDownChoiceColumn(Model.of("Node type"), "userObject.xsltNodeType", XsltNodeType.getValues()) {
			@Override
			public String getCellCssClass(IModel rowModel, int rowNum) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) rowModel.getObject();
				String styleClass = ((FillingRef) node.getUserObject()).getXsdStyleClass();
				return Strings.isEmpty(styleClass) ? super.getCellCssClass(rowModel, rowNum) : styleClass;
			}
		});

		final TreeGrid<DefaultTreeModel, DefaultMutableTreeNode> grid = new TreeGrid<DefaultTreeModel, DefaultMutableTreeNode>("grid", model, columns) {
			private static final long serialVersionUID = 1L;

			@Override
			public void selectItem(IModel<DefaultMutableTreeNode> itemModel, boolean selected) {
				DefaultMutableTreeNode node = itemModel.getObject();
				((FillingRef) node.getUserObject()).setSelected(selected);
				if (!selected) {
					((FillingRef) node.getUserObject()).setStyleRefNeed(false);
				}
				super.selectItem(itemModel, selected);
			}

			@Override
			public boolean isItemSelected(IModel<DefaultMutableTreeNode> itemModel) {
				DefaultMutableTreeNode node = itemModel.getObject();
				selectItem(itemModel, ((FillingRef) node.getUserObject()).isSelected());
				return ((FillingRef) node.getUserObject()).isSelected();
			}
		};

		grid.setContentHeight(35, SizeUnit.EM);
		grid.setAllowSelectMultiple(true);
		grid.setClickRowToSelect(true);
		grid.setClickRowToDeselect(false);
		grid.setSelectToEdit(true);
		grid.getTreeState().expandAll();

		form.add(grid);

		IModel<String> selectedItemsModel = new Model<String>()
		{
			private static final long serialVersionUID = 1L;

			@Override
			public String getObject()
			{
				return selectedItemsAsString(grid);
			}
		};
		// form.add(selectionLabel = new Label("currentSelection", selectedItemsModel));
		// selectionLabel.setOutputMarkupId(true);

	}

	@Override
	public void onEvent(IEvent<?> event) {
		// TODO Auto-generated method stub
		super.onEvent(event);
	}

	private String selectedItemsAsString(TreeGrid<DefaultTreeModel, DefaultMutableTreeNode> grid)
	{
		StringBuilder res = new StringBuilder();
		Collection<IModel<DefaultMutableTreeNode>> selected = grid.getSelectedItems();
		for (IModel<DefaultMutableTreeNode> model : selected)
		{
			DefaultMutableTreeNode node = model.getObject();
			FillingRef fillingRef = (FillingRef) node.getUserObject();
			if (res.length() > 0)
			{
				res.append(", ");
			}
			res.append(fillingRef.getXpath() + fillingRef.getName());
		}
		return res.toString();
	}

}
